//
//  XHLoginViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-16.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHLoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "XHRegisterViewController.h"
#import "XHFindPwdViewController.h"
#import "XHLoginViewModel.h"
#import "MBProgressHUD.h"

@interface XHLoginViewController()
@property (nonatomic ,strong) IBOutlet UITextField *usernameTextField;
@property (nonatomic ,strong) IBOutlet UITextField *passwordTextField;
@property (nonatomic ,strong) IBOutlet UIButton *loginButton;
@property (nonatomic ,strong) IBOutlet UIButton *registerButton;
@property (nonatomic ,strong) IBOutlet UIButton *forgetPasswordButton;

@property (nonatomic ,strong) XHLoginViewModel *viewModel;
@property (nonatomic ,strong) MBProgressHUD *HUD;


@end

@implementation XHLoginViewController


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
        @weakify(self);
        self.rightButton.hidden = YES;
       
        [self.leftButton setImage:[UIImage imageNamed:@"showLeft.png"] forState:UIControlStateNormal];
        self.leftButton.imageEdgeInsets = UIEdgeInsetsMake(10, 13, 5, 5);
        //self.leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
        
        self.titleLable.text =  @"登录";
        
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"LoginView" owner:self options:nil];
        UIView *view = [array objectAtIndex:0];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
        [self.view addSubview:view];
        self.loginButton.layer.masksToBounds = YES;
        self.loginButton.layer.cornerRadius = 6.0f;
         [self.loginButton setBackgroundImage:[UIImage imageNamed:@"button_unactive.png"] forState:UIControlStateDisabled];
        [[self.forgetPasswordButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            XHFindPwdViewController *findPwdViewController = [[XHFindPwdViewController alloc] init];
            @strongify(self);
            [self presentViewController:findPwdViewController animated:YES completion:nil];
        }];
      
        [[self.registerButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            XHRegisterViewController *registerViewController = [[XHRegisterViewController alloc] init];
            [self presentViewController:registerViewController animated:YES completion:nil];
        }];
        
        [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            self.HUD.labelText = @"正在登录";
            [self.HUD show:YES];
            [self.passwordTextField resignFirstResponder];
            [self.viewModel doLogin];
        }];
        
        
        self.viewModel = [[XHLoginViewModel alloc] init];
        
        RAC(self.viewModel,username) = self.usernameTextField.rac_textSignal;
        RAC(self.viewModel,password) = self.passwordTextField.rac_textSignal;
        
        
        
        RAC(self.loginButton,enabled) = [RACSignal combineLatest:@[self.usernameTextField.rac_textSignal, self.passwordTextField.rac_textSignal] reduce:^id(NSString *username, NSString *password){
            
            return @(username.length > 0 && password.length > 0);
        }];

        self.HUD = [[MBProgressHUD alloc] initWithView:view];
        self.HUD.dimBackground = YES;
        [view addSubview:self.HUD];
        
        [RACObserve(self.viewModel, hasLogin) subscribeNext:^(id x) {
            if ([x integerValue]) {
                self.HUD.labelText = @"登录成功";
                [self.HUD showAnimated:YES whileExecutingBlock:^{
                    sleep(3);
                } completionBlock:^{
                    butoncl* al = [[butoncl alloc] init];
                    [al postString:@"015"fff:@""];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"Login" object:nil];
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                
            }
        }];
        
        [RACObserve(self.viewModel, showError) subscribeNext:^(id x) {
            if ([x integerValue]) {
                @strongify(self);
                
                self.HUD.labelText = @"用户名或密码错误";
               [self.HUD showAnimated:YES whileExecutingBlock:^{
                   sleep(2);
               } completionBlock:^{
                   
               }];
            }
        }];
        
        [RACObserve(self.viewModel, showNetError) subscribeNext:^(id x) {
            if ([x integerValue]) {
                @strongify(self);
                
                self.HUD.labelText = @"网络连接失败";
                [self.HUD showAnimated:YES whileExecutingBlock:^{
                    sleep(2);
                } completionBlock:^{
                    
                }];
            }
        }];

        
    }
    return self;
}


@end
